package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    // 发布动态
    @PostMapping("/movements")
    public void saveMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        // 调用manager
        movementManager.saveMovement(movement, imageContent);
    }

    // 查看我的动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovement(
            Long userId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findMyMovement(userId, pageNum, pageSize);
    }

    // 查看好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findFriendMovement(pageNum, pageSize);
    }

    // 查看推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findRecommendMovement(pageNum, pageSize);
    }

    // 动态点赞
    @GetMapping("/movements/{publishId}/like")
    public ResponseEntity setMovementLike(@PathVariable String publishId) {
        // 调用mananger
        return movementManager.setMovementLike(publishId);
    }

    // 动态取消点赞
    @GetMapping("/movements/{publishId}/dislike")
    public ResponseEntity removeMovementLike(@PathVariable String publishId) {
        // 调用mananger
        return movementManager.removeMovementLike(publishId);
    }

    // 动态喜欢
    @GetMapping("/movements/{publishId}/love")
    public ResponseEntity setMovementLove(@PathVariable String publishId) {
        // 调用mananger
        return movementManager.setMovementLove(publishId);
    }

    // 动态取消喜欢
    @GetMapping("/movements/{publishId}/unlove")
    public ResponseEntity removeMovementLove(@PathVariable String publishId) {
        // 调用mananger
        return movementManager.removeMovementLove(publishId);
    }

    // 单条动态（详情）  /movements/visitors
    @GetMapping("/movements/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId) {
        // 调用manager
        return movementManager.findMovementVoById(publishId);
    }

    // 动态评论列表
    @GetMapping("/comments")
    public ResponseEntity findMovementCommentByPage(@RequestParam("movementId") String publishId,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findMovementCommentByPage(publishId, pageNum, pageSize);
    }

    // 发表动态评论
    @PostMapping("/comments")
    public void saveMovementComment(@RequestBody Map<String, String> map) {
        // 获取参数
        String publishId = map.get("movementId");  // 动态id
        String content = map.get("comment"); // 评论内容
        // 调用manager
        movementManager.saveMovementComment(publishId, content);
    }
}
